package com.newtouch.bxzs.business.whiteList.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.enclosure.controller.EnclosureController;
import com.newtouch.bxzs.business.product.model.ProductBO;
import com.newtouch.bxzs.business.whiteList.mapper.WhiteListMapper;
import com.newtouch.bxzs.business.whiteList.model.WhiteListBO;
import com.newtouch.bxzs.business.whiteList.model.WhiteListVO;
import com.newtouch.bxzs.business.whiteList.service.WhiteListService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.PublishStatusEnum;
import com.newtouch.bxzs.common.base.model.Attachment;
import com.newtouch.bxzs.common.base.model.Product;
import com.newtouch.bxzs.common.base.model.WhiteList;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 白名单
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年8月18日</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author Amour
 *
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class WhiteListServiceImpl implements WhiteListService {

    @Autowired
    private WhiteListMapper whiteListMapper;

    @Autowired
    private RedisTemplate<String, List<WhiteListBO>> redisTemplate1;




    @Override
    public IPage<WhiteListBO> getWhiteList(WhiteListVO whiteListVO) {
        QueryWrapper<WhiteList> query = new QueryWrapper<>();
        query.eq("IS_DELETE", 0);
        query.orderByDesc("CREATE_TIME");

        Page<WhiteList> p = new Page<>(whiteListVO.getCurrentPage(), whiteListVO.getPageSize());
        IPage<WhiteList> whiteListPage = whiteListMapper.selectPage(p, query);
        if (whiteListPage == null) {
            return null;
        }
        IPage<WhiteListBO> page = new Page<>();

        page.setRecords(EntityToBeanUtils.copyBean(whiteListPage.getRecords(), WhiteListBO.class));
        page.setCurrent(whiteListPage.getCurrent());
        page.setPages(whiteListPage.getPages());
        page.setSize(whiteListPage.getSize());
        page.setTotal(whiteListPage.getTotal());
        return page;
    }

    @Override
    public Integer modifyWhiteListUser(WhiteList whiteList) {
        if(StringUtils.isEmpty(whiteList.getWhiteListId())){
            /*设置创建人ID*/
            whiteList.setCreateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
            /*创建时间*/
            whiteList.setCreateTime(new Date());
            return whiteListMapper.insert(whiteList);
        }else{
            /*设置修改人ID*/
            whiteList.setUpdateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
            /*设置修改时间*/
            whiteList.setUpdateTime(new Date());
            return whiteListMapper.updateById(whiteList);
        }
    }

    @Override
    public String updateDelState(List<String> whiteListIds) {
        if(CollectionUtils.isEmpty(whiteListIds)){
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        for (String id : whiteListIds){
            whiteListMapper.deleteById(id);
        }
        return "删除成功";
    }

    @Override
    public List<WhiteListBO> getWhiteList1() {

        List<WhiteListBO> list = redisTemplate1.opsForValue().get("white");
        if (list != null && list.size() >= 1) {
            return list;
        }
        this.redisSet("white");
        return whiteListMapper.getWhiteList1();
    }

    public void redisSet(String tey) {
        redisTemplate1.opsForValue().set(tey, whiteListMapper.getWhiteList1());
    }

}
